import { useCallback } from 'react';

import {
  useParams, useLocation, useHistory, useRouteMatch
} from 'react-router-dom';

import { useIntl } from 'react-intl';

import locals from '@/locales/zh';

export * from './model';
export * from './table';

// 导出路由中的hooks
export {
  useParams, useLocation, useHistory, useRouteMatch
};

export type IntlKey = keyof typeof locals;

export const useI18n = () => {
  const _intl = useIntl();
  const intl = useCallback(
    (id: IntlKey, value?: Record<string, any>) => {
      try {
        return _intl.formatMessage({ id }, value);
      } catch (e) {
        return id;
      }
    },
    [_intl]
  );

  return intl;
};
